package com.wandian.texas.command.texas;

import java.util.List;

import com.wandian.texas.BaseCommandObject;
import com.wandian.texas.UserMgr;
import com.wandian.command.Cmd;
import com.wandian.command.UserCommand;
import com.wandian.texas.db.DaoManager;
import com.wandian.texas.entity.FriendsEntity;
import com.wandian.texas.protocol.Protocol;

@Cmd(code = Protocol.RequestFriendListCmd, desc = "好友列表")
public class FriendListCmd extends UserCommand {

	@Override
	public void execute(BaseCommandObject cmdObject) {
		super.execute(cmdObject);
		// byte roomid = cmdObject.readByte();
		sendFriendList(cmdObject);
	}

	/**
	 * 将房间内玩家列表信息发送过去
	 * 
	 * @param cmdObject
	 */
	private void sendFriendList(BaseCommandObject cmdObject) {
		if (cmdObject.getUser() == null)
			return;
		List<FriendsEntity> friends = DaoManager.FriendsDao(cmdObject.dbIndex).getFriendList(cmdObject.getUser().userId);
		int userCount = 0;
		cmdObject.writeBegin();
		// int userLengthPos = cmdObject.position();
		if (friends != null && friends.size() > 0) {
			userCount = friends.size();
		}
		cmdObject.writeInt(userCount);
		FriendsEntity user;
		for (int i = 0; i < userCount; i++) {
			user = friends.get(i);
			cmdObject.writeInt(user.UserId);
			cmdObject.writeString(user.Icon);
			cmdObject.writeString(user.NickName);
			cmdObject.writeBytes(user.HeadImage);
			cmdObject.writeByte((byte) user.Sex);
			cmdObject.writeFloat(user.Money);
			cmdObject.writeByte((byte) user.IsPlaying);
			cmdObject.writeInt(user.CurrentRoomId);
		}
		cmdObject.writeEnd();
		UserMgr.I().SendData(cmdObject);
	}
}
